<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Worker; 

class WMyreport extends Base
{
    public function W_myreport()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if(!($system['ruanjian']!=3 || $config['showshop']==0 || ($system['ruanjian']==3 && $config['showshop']==$myworker['company']))){
	$myworker = "";
}
if (empty($myworker)) {
	return_msg('您不是员工,无权访问');
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}

//获取每天的开始时间
// if (date("H")>=intval($config['todaytime'])){
// 	$todaytime = date("Y-m-d") . " " . intval($config['todaytime']) . ":00:00";
// }else{
// 	$todaytime = date("Y-m-d",strtotime("-1 day")) . " " . intval($config['todaytime']) . ":00:00";
// }

$todaytime = strtotime(date("Y-m-d 00:00:00"));


$do = 'myreport';

//读取顶部个人信息
if (!empty($myworker['company'])) {
	$shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$myworker['company']}'");
	$shopname=$shop['name'];
}
if (!empty($myworker['team'])) {
	$team=db_find("SELECT * FROM ".tablename(t_team)." WHERE id= '{$myworker['team']}'");
	$teamname=$team['name'];
}
$row['uid'] = $_G['member']['uid'];

$paycount = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
		WHERE status > 0  and status!=3 and workerid = '{$row['uid']}' and themeid = '{$themeid}' " );

$unpaidcount = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
		WHERE status > 0  and status!=3 and workerid = '{$row['uid']}' and themeid = '{$themeid}' " );

$paycounttoday = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
		WHERE status > 0  and status!=3 and workerid = '{$row['uid']}' and addtime>'{$todaytime}' and themeid = '{$themeid}' " );
$unpaidcounttoday = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
		WHERE status > 0  and status!=3 and workerid = '{$row['uid']}' and addtime>'{$todaytime}' and themeid = '{$themeid}'  " );

$directcount = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
		WHERE status > 0  and status!=3 and fromuid = '{$row['uid']}' and themeid = '{$themeid}' and workerid = '{$row['uid']}'" );
$directcounttoday = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
		WHERE status > 0  and status!=3 and fromuid = '{$row['uid']}' and addtime>'{$todaytime}' and themeid = '{$themeid}' and workerid = '{$row['uid']}'" );


if ($myworker['level'] == '1'){	
	$shenfen="超级管理员";
}elseif ($myworker['level'] == '2'){	
	$shenfen="商家管理员";
}else{	
	$shenfen="员工";
}

if ($myworker['level'] == '1' || ($myworker['level'] == '2' && $config['showshop']>0 )){
	$teampaycount = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
					WHERE themeid ='{$themeid}'  and status > 0  and status!=3 " ));
	$teampaycounttoday = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
				WHERE themeid ='{$themeid}'  and addtime>'{$todaytime}' and status > 0  and status!=3 " ));
	$teamtips ="所有";
}elseif ($myworker['level'] == '2'){
	$teampaycount = intval(db_fetchcolumn ( "SELECT sum(r.record) FROM " . tablename ( t_worker ) . " w
	left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid
	WHERE w.themeid ='{$themeid}' and w.company = " . $myworker['company'] ." order by riqi desc" ));

	$teampaycounttoday = intval(db_fetchcolumn ( "SELECT sum(r.record) FROM " . tablename ( t_worker ) . " w
	left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid
	WHERE w.themeid ='{$themeid}' and w.company = " . $myworker['company'] ." and r.riqi='".date("Y-m-d"). "' order by riqi desc" ));
	$teamtips ="商家";
}

$myreport = db_select("SELECT * FROM " . tablename ( t_workerreport ) . " WHERE themeid ='{$themeid}' and workerid = '{$_G['member']['uid']}' order by riqi desc" );

if ($myworker['level'] != '3' || ($myworker['level'] == '3' && $config['isreport']==1)){
	$myteam = db_select("SELECT r.riqi, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
		left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid
		WHERE w.themeid ='{$themeid}' and w.team = " . $myworker['team'] ." and w.company = " . $myworker['company'] ." group by r.riqi order by riqi desc" );

	$mycompany = db_select("SELECT r.riqi, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
		left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid
		WHERE w.themeid ='{$themeid}' and w.company = " . $myworker['company'] ." group by r.riqi order by riqi desc" );

	$notworker = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
		WHERE status > 0  and status!=3 and workerid = '' and themeid = '{$themeid}' " );
}
if($_G['companyname'] =='新飞驰软件'){
	$return_clicent = get_defined_vars();
return view('/fcworker/fc_myreport' , $return_clicent);
}else{
	$return_clicent = get_defined_vars();
return view('/yxworker/w_myreport' , $return_clicent);
}
}
}